package com.zlq.cn.controller.api;


import com.zlq.cn.annotations.ApiParamBean;
import com.zlq.cn.api.ApiMessage;
import com.zlq.cn.api.ApiParam;
import com.zlq.cn.dto.param.RiddlePageParam;
import com.zlq.cn.http.RouteApiConfig;
import com.zlq.cn.service.un.RiddleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhoucl
 * @since 2021-09-02
 */
@Slf4j
@RestController
@RequestMapping("/riddle")
public class RiddleController {

    @Resource
    private RiddleService riddleService;

    @Resource
    private RouteApiConfig.ShowApiService showApiService;

    @PostMapping("/page")
    public Object page(ApiParam apiParam, @ApiParamBean RiddlePageParam riddlePageParam) {
        return riddleService.list(riddlePageParam);
    }

    @GetMapping("/sync")
    public Object sync() {
        /*try {
            String[] typeIds = new String[]{
                    "gxmy", "zmmy", "dwmy", "cymy", "aqmy", "dmmy", "rmmy", "dimmy", "cy", "dgmy", "ry", "etmy", "wpmy",
                    "zwmy", "jmmy", "sbmy", "symy", "ypmy", "yymy", "ysmy", "cwmy", "qtmy", "miyujingxuan", "njmy", "zlmy"
            };
            List<Riddle> riddles = StreamEx.of(typeIds)
                    .map(typeId -> {
                        List<Riddle> riddleList = new ArrayList<>();
                        try {
                            int page = 1;
                            RouteApiConfig.ShowApiCommonResult<RouteApiConfig.RiddleResult<Riddle>> result = showApiService.query(typeId, page).execute().body();
                            RouteApiConfig.RiddlePageResult<Riddle> pb = result.getShowapiResBody().getPb();
                            if (CollectionUtils.isNotEmpty(pb.getContentlist())) {
                                riddleList.addAll(pb.getContentlist());
                            }
                            while (page <= pb.getAllPages()) {
                                Thread.sleep(500);
                                result = showApiService.query(typeId, page).execute().body();
                                pb = result.getShowapiResBody().getPb();
                                riddleList.addAll(pb.getContentlist());
                                page += 1;
                            }
                            System.out.println("page===>" + page);
                            return riddleList;
                        } catch (Exception e) {
                            log.error("同步失败", e);
                        }
                        return riddleList;
                    })
                    .flatMap(Collection::stream)
                    .toList();

            riddleService.saveBatch(riddles);
        } catch (Exception e) {
            log.error("同步失败", e);
        }*/
        return ApiMessage.resultApiMessage("同步成功");
    }
}

